package com.ruoyi.project.his.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.his.domain.DoctorDept;
import com.ruoyi.project.his.serivce.IDoctorDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 科室管理控制器
 */
@RestController
@RequestMapping("/his/dept") // 前缀路径
public class DoctorDeptController extends BaseController {

    @Autowired
    private IDoctorDeptService doctorDeptService;

    /**
     * 查询所有科室
     * @return
     */
    @PreAuthorize("@ss.hasPermi('his:dept:list')")
    @GetMapping("/all")
    public AjaxResult listDoctorDept(){
        return AjaxResult.success(doctorDeptService.selectDoctorDeptAll());
    }

    /**
     * 分页查询
     * @param doctorDept 查询条件
     * @return 若依封装的，分页查询结果类型。包括当前页面数据集合和总计数据行数
     */
    @PreAuthorize("@ss.hasPermi('his:dept:list')")
    @GetMapping("/list")
    public TableDataInfo listDoctorDept(DoctorDept doctorDept){
        // 开启分页
        startPage();
        // 查询科室
        List<DoctorDept> list = doctorDeptService.selectDoctorDeptList(doctorDept);
        // 返回，使用父类型中的方法，把查询结果处理后再返回
        return getDataTable(list);
    }

    /**
     * 根据主键查询科室详情
     * @param deptId
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi('his:dept:query')")
    @GetMapping("/{deptId}")
    public AjaxResult getInfo(@PathVariable Integer deptId){
        return AjaxResult.success(doctorDeptService.selectDoctorDeptByDeptId(deptId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('his:dept:add')")
    @Log(title = "科室管理", businessType = BusinessType.INSERT) // 记录操作日志
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DoctorDept doctorDept)    {
        if (UserConstants.NOT_UNIQUE.equals(doctorDeptService.checkDocketDeptNameUnique(doctorDept))){ // 科室名称唯一
            return AjaxResult.error("新增科室'" + doctorDept.getDeptName() + "'失败，科室名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(doctorDeptService.checkDocketDeptCodeUnique(doctorDept))){ // 科室编码唯一
            return AjaxResult.error("新增科室'" + doctorDept.getDeptCode() + "'失败，科室编码已存在");
        }
        doctorDept.setCreateBy(SecurityUtils.getUsername()); // 设置创建者姓名。从security管理的登录用户中获取
        return toAjax(doctorDeptService.insertDoctorDept(doctorDept));
    }

    /**
     * 更新岗位
     */
    @PreAuthorize("@ss.hasPermi('his:dept:modify')")
    @Log(title = "科室管理", businessType = BusinessType.UPDATE) // 记录操作日志
    @PutMapping
    public AjaxResult modify(@Validated @RequestBody DoctorDept doctorDept)    {
        if (UserConstants.NOT_UNIQUE.equals(doctorDeptService.checkDocketDeptNameUnique(doctorDept))){ // 科室名称唯一
            return AjaxResult.error("更新科室'" + doctorDept.getDeptName() + "'失败，科室名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(doctorDeptService.checkDocketDeptCodeUnique(doctorDept))){ // 科室编码唯一
            return AjaxResult.error("更新科室'" + doctorDept.getDeptCode() + "'失败，科室编码已存在");
        }
        doctorDept.setUpdateBy(SecurityUtils.getUsername()); // 设置更新者姓名。从security管理的登录用户中获取
        return toAjax(doctorDeptService.updateDoctorDept(doctorDept));
    }

    /**
     * 批量删除科室
     * @param deptIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('his:dept:remove')")
    @Log(title = "科室管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public AjaxResult batchDelete(@PathVariable Integer[] deptIds){
        return toAjax(doctorDeptService.deleteDoctorDeptByDeptIds(deptIds));
    }

}
